/**
 * Created by lwy on 2017/8/4.
 */

var page = 1;
var id;
var phone;
var name;
var state;


function doAjax(){
    $.ajax({
        url:'/crm/goodsReserveC/search.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            pageNu:page,
            phone:phone,
            name:$('#creatUser').val(),
            type:$('#type').val(),
            state:$('#state').val(),
        },
        type:'post',
        success: function (data) {
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==0){
                alert(data.msg);
            }
            if(data.code==1){
                $('#record').html(data.datas.recordsTotal);
                $('#withdraw-record').empty();
                for(var i=0;i<data.datas.goodsReserveList.length;i++){
                    $('<tr class="gradeA odd">'+
                        '<a href=""><td class="sorting_1 withdrawId" style="text-decoration: underline;cursor: pointer;" data-toggle="modal" data-target="#myModal5" id="'+data.datas.goodsReserveList[i].id+'">'+data.datas.goodsReserveList[i].id+'</td></a>'+
                        '<td class=" ">'+data.datas.goodsReserveList[i].name+'</td>'+
                        '<td class=" ">'+data.datas.goodsReserveList[i].phone+'</td>'+
                        '<td class="center ">'+data.datas.goodsReserveList[i].num+'</td>'+
                        '<td class="sorting_1">'+data.datas.goodsReserveList[i].type+'</td>'+
                        '<td class=" ">'+data.datas.goodsReserveList[i].createTime+'</td>'+
                        '<td class=" ">'+data.datas.goodsReserveList[i].modifyTime+'</td>'+
                        '<td class=" ">'+data.datas.goodsReserveList[i].ifHanded+'</td>'+
                        '</tr>').appendTo($('#withdraw-record'));
                }
                //id处理
                $('.withdrawId').click(function(){
                    id = $(this).attr('id');
                    $.ajax({
                        url:'/crm/goodsReserveC/getOne.json',
                        dataType:'json',
                        data:{
                            token:localStorage.getItem('crmtoken'),
                            id:id,
                        },
                        type:'post',
                        success:function(data){
                            if(data.code==0){
                                alert(data.msg)
                                return;
                            }
                            if(data.code==-1){
                                window.parent.location.href="/crm/login.jsp"
                            }
                            if(data.code==1){
                                $('#reserveName').val(data.datas.name);
                                $('#reservePhone').val(data.datas.phone);
                                $('#reserveNum').val(data.datas.num);
                                $('#reserveType').val(data.datas.type);
                                $('#reserveState').val(data.datas.ifHanded);
                                $('#reserveTime').val(data.datas.createTime);
                                $('#handleTime').val(data.datas.modifyTime);
                                $('#handleUSer').val(data.datas.handUser);
                                $('#handleResult').val(data.datas.resultMemo);
                                if(data.datas.ifHanded=='订购申请'){
                                    $('#save').show();
                                    $('#nosave').show();
                                    $('#handlertips').attr('readonly',false);
                                }else{
                                    $('#save').hide();
                                    $('#nosave').hide();
                                    $('#handlertips').attr('readonly',true);
                                }
                                //if(data.datas.ifHanded=='已处理'){
                                //    $('#save').hide();
                                //    $('#nosave').hide();
                                //    $('#handlertips').attr('readonly',true);
                                //}else{
                                //    $('#save').show();
                                //    $('#nosave').show();
                                //    $('#handlertips').attr('readonly',false);
                                //}
                            }
                        }
                    })
                })
                //通过
                $('#save').unbind('click').click(function(){
                    $.ajax({
                        url:'/crm/goodsReserveC/edit.json',
                        dataType:'json',
                        data:{
                            token:localStorage.getItem('crmtoken'),
                            id:id,
                            resultMemo:$('#handlertips').val(),
                            state:3,
                        },
                        type:'post',
                        success:function(data){
                            if(data.code==0){
                                alert(data.msg)
                                return;
                            }
                            if(data.code==-1){
                                window.parent.location.href="/crm/login.jsp"
                            }
                            if(data.code==1){
                                alert('处理成功');
                                $('#myModal5').removeClass('fade in');
                                $('#myModal5').addClass('fade');
                                $('#myModal5').attr('aria-hidden',true);
                                $('#myModal5').css('display','none');
                                $('.modal-backdrop').css('display','none');
                                page=1;
                                doAjax();
                            }
                        }
                    })
                })
                //不通过
                $('#nosave').unbind('click').click(function(){
                    $.ajax({
                        url:'/crm/goodsReserveC/edit.json',
                        dataType:'json',
                        data:{
                            token:localStorage.getItem('crmtoken'),
                            id:id,
                            resultMemo:$('#handlertips').val(),
                            state:2,
                        },
                        type:'post',
                        success:function(data){
                            if(data.code==0){
                                alert(data.msg)
                                return;
                            }
                            if(data.code==-1){
                                window.parent.location.href="/crm/login.jsp"
                            }
                            if(data.code==1){
                                alert('处理成功');
                                $('#myModal5').removeClass('fade in');
                                $('#myModal5').addClass('fade');
                                $('#myModal5').attr('aria-hidden',true);
                                $('#myModal5').css('display','none');
                                $('.modal-backdrop').css('display','none');
                                page=1;
                                doAjax();
                            }
                        }
                    })
                })
                //分页
                $(".pagination").createPage({
                    totalPage:data.datas.totalPage,
                    currPage:data.datas.pageNu,
                    pageCount:data.datas.pageNu,
                    turndown:'true',
                    backFn:function(p){
                        page=p;
                        doAjax();
                    }
                });
            }
        }
    })
}

$(function(){
    doAjax();
})


//查询
$('#refer').click(function(){
    phone = $('#phone').val();
    name = $('#creatUser').val();
    page=1;
    doAjax();
})

